package daily.play2;

//守护线程
/*class MyThread1 extends Thread{
    public void run(){
        for (int i = 0; i < 10; i++) {
            System.out.println(getName()+i);
        }
    }
}
class MyThread2 extends Thread{
    public void run(){
        for (int i = 0; i < 100; i++) {
            System.out.println(getName()+i);
        }
    }
}
public class review {
    public static void main(String[] args) {
        MyThread1 t1  = new MyThread1();
        MyThread2 t2 = new MyThread2();
        t1.setName("女神");
        t2.setName("备胎");
        t2.setDaemon(true);
        t1.start();
        t2.start();
    }
}*/


/*需求：
某电影院目前正在上映国产大片，共有100张票，而它有3个窗口卖票，请设计一个程序模拟该电影院卖票
*/
/*
class MyThread implements Runnable{
    public static int ticket = 0;
    public void run(){
        while(true){
            synchronized (MyThread.class){
                if(ticket>=100){
                    break;
                }else{
                    ticket++;
                    System.out.println(Thread.currentThread().getName()+"正在卖第"+ticket+"张票");
                }
            }
        }
    }
}
public class review{
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyThread());
        Thread t2 = new Thread(new MyThread());
        t1.setName("窗口1");
        t2.setName("窗口2");
        t1.start();
        t2.start();
    }
}*/


/*
import java.util.concurrent.locks.ReentrantLock;

class MyThread implements Runnable{
    public static int ticket = 0;
    static ReentrantLock lock = new ReentrantLock();
    public void run(){
        while(true){
                try{
                    lock.lock();
                    if(ticket>=100){
                        break;
                    }else{
                        ticket++;
                        System.out.println(Thread.currentThread().getName()+"正在卖第"+ticket+"张票");
                    }
                }finally {
                    lock.unlock();
                }
        }
    }
}
public class review{
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyThread());
        Thread t2 = new Thread(new MyThread());
        t1.setName("窗口1");
        t2.setName("窗口2");
        t1.start();
        t2.start();
    }
}*/


/*
import java.util.concurrent.locks.ReentrantLock;

class MyThread implements Runnable{
    public static int ticket = 0;
    static ReentrantLock lock = new ReentrantLock();
    public void run(){
        while(true){
            try{
                lock.lock();
                if(ticket>=100){
                    break;
                }else{
                    ticket++;
                    System.out.println(Thread.currentThread().getName()+"正在卖第"+ticket+"张票");
                }
            }finally {
                lock.unlock();
            }
        }
    }
}
public class review{
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyThread());
        Thread t2 = new Thread(new MyThread());
        t1.setName("窗口1");
        t2.setName("窗口2");
        t1.start();
        t2.start();
    }
}*/

/*import java.util.concurrent.locks.ReentrantLock;

class MyThread implements Runnable{
    public static int ticket = 0;
    static ReentrantLock lock = new ReentrantLock();
    public void run(){
        while(true){
            if (wwl()) break;
        }
    }

    public static synchronized boolean wwl() {
        if(ticket>=100){
            return true;
        }else{
            ticket++;
            System.out.println(Thread.currentThread().getName()+"正在卖第"+ticket+"张票");
        }
        return false;
    }
}
public class review{
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyThread());
        Thread t2 = new Thread(new MyThread());
        t1.setName("窗口1");
        t2.setName("窗口2");
        t1.start();
        t2.start();
    }
}*/

import java.util.concurrent.ArrayBlockingQueue;

//阻塞队列
class desk{
    public static int flagfood = 10;
    public static Object  lock = new Object();
}
class cook implements Runnable{
    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
    public cook(ArrayBlockingQueue<String> queue){
        this.queue = queue;
    }
    public void run(){
        while(true){
            if(desk.flagfood<=0){
                break;
            }
            try {
                queue.put("蛋糕");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("厨师做了一个蛋糕");
        }
    }
}
class food implements Runnable{
    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
    public food(ArrayBlockingQueue<String> queue){
        this.queue = queue;
    }
    public void run(){
        while(true){
            if(desk.flagfood<=0){
                break;
            }
            try {
                desk.flagfood--;
                queue.take();

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("吃货吃了一个蛋糕");
        }
    }
}
public class reviewdemo2{
    public static void main(String[] args) {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
        cook cook = new cook(queue);
        food food = new food(queue);
        Thread t1= new Thread(food);
        Thread t2= new Thread(cook);
        t1.start();
        t2.start();
    }
}